﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using IDAL;
using Model;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using System.Linq;

namespace DAL
{
    public class BaseDal<T> : IBaseDal<T> where T : BaseEntity,new()
    {
        private MySchooldbContext _schooldbContext;
        public BaseDal(MySchooldbContext schooldbContext) 
        {
        _schooldbContext= schooldbContext;
        }
        public async Task<int> AddAsync(T model) 
        {
            _schooldbContext.Entry<T>(model).State = EntityState.Added;
            return await _schooldbContext.SaveChangesAsync();
        }
        public async Task<int> UpdateAsync(T model) 
        {
            _schooldbContext.Entry(model).State = EntityState.Deleted;
            return await _schooldbContext.SaveChangesAsync();
        }
        public async Task<int> DeleteAsync(T model) 
        {
            _schooldbContext.Entry(model).State = EntityState.Modified;
            return await _schooldbContext.SaveChangesAsync();
        }
        public async Task<List<T>> GetAll() 
        {
        return await _schooldbContext.Set<T>().ToListAsync();
        }
        public async Task<T> GetIdAll(Guid id) 
        {
            return await _schooldbContext.Set<T>().FindAsync(id);
        }
        public async Task<List<T>> GetDataByChoose(Expression<Func<T, bool>> whereLambda)
        {
            return await _schooldbContext.Set<T>().Where(whereLambda).ToListAsync();
        }
    }
}
